<template>
    <ContentWrap>
        <h2 class="fonts24 fontw500">报名表模板</h2>
        <div class="flex justify-between">
            <div class="flex aic">
                <ElButton type="primary" :icon="Plus" @click="onAdd">
                    {{ t('新增报名表模板') }}
                </ElButton>
            </div>
            <div class="flex aic">
                <ElInput v-model="state.keywords" style="width: 266px;height: 32px;margin-right: 10px;"
                    placeholder="搜索报名表模板名称" :prefix-icon="Search" />
                <ElButton type="primary" :icon="Search" @click="onSearch">
                    查询
                </ElButton>
                <ElButton :icon="Refresh" @click="onReset">
                    重置
                </ElButton>
            </div>
        </div>
        <div class="" style="margin-top: 25px;">
            <Table v-model:pageSize="pageSize" v-model:currentPage="currentPage" :columns="allSchemas.tableColumns"
                :data="dataList" :loading="loading" :pagination="{ total: total }" @register="tableRegister"
                :headerCellStyle="headerCellStyle" :rowStyle="rowStyle" />
        </div>
    </ContentWrap>
    <Dialog v-model="dialogVisible1" :title="state.dialogVisible1Title" width="1200" >
        <el-form v-loading="loadings" ref="ruleFormRef" label-position="top" :model="state.ruleForm" :rules="rules" label-width="120px"
            class="demo-ruleForm" status-icon>
            <el-form-item label="报名表模板名称：" prop="templateName">
                <div class="flex jcsb aic">
                    <el-input v-model="state.ruleForm.templateName" placeholder="请输入报名表名称" />
                </div>
            </el-form-item>
            <el-form-item label="报名表模板配置信息：" prop="">
                <div class="" style="width: 100%;height: 100%;">
                    <FromTable :data="state.ruleForm.templateValue" @FromTableClick="FromTableClick"/>
                </div>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    保存
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">取消</el-button>
            </el-form-item>
        </el-form>
    </Dialog>
</template>
    
<script setup lang="tsx">
import FromTable from "./components/FromTable.vue"
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { Search, Plus, Refresh, Pointer, Star } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import { Dialog } from '@/components/Dialog'
import { useEmitt } from '@/hooks/event/useEmitt'
import { ElButton, ElInput, ElMessage, ElMessageBox } from 'element-plus'
// import { propTypes } from '@/utils/propTypes'
import { useI18n } from '@/hooks/web/useI18n'
import { ContentWrap } from '@/components/ContentWrap'
import { ref, reactive, unref } from "vue"
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useForm } from '@/hooks/web/useForm'
import { useValidator } from '@/hooks/web/useValidator'
import { formTemplateList, addformTemplateApi, editformTemplateApi, deletepositionApi } from "@/api/resume/index"
import { formatToDateTime } from "@/utils/dateUtil"
const { required } = useValidator()
const { formRegister, formMethods } = useForm()
const { getElFormExpose } = formMethods
const { t } = useI18n()
const dialogVisible1 = ref(false)
const loadings = ref(false)
const state = reactive({
    ruleForm: {} as any,
    react: false,
    keywords: "",
    dialogVisible1Title: "新增报名表模板"
})
// 表格基本信息
const crudSchemas = reactive<CrudSchema[]>([
    {
        field: 'index',
        label: t('序号'),
        type: 'index',
        width: '260',
    },
    {
        field: 'templateName',
        label: t('报名表模板名称'),
        search: {
            hidden: true
        }
    },
    {
        field: 'action',
        width: '260px',
        label: t('tableDemo.action'),
        search: {
            hidden: true
        },
        form: {
            hidden: true
        },
        detail: {
            hidden: true
        },
        table: {
            slots: {
                default: (data: any) => {
                    return (
                        <>
                            <ElButton link type="primary" onClick={() => action(data[0].row)}>
                                编辑
                            </ElButton>
                            <ElButton link type="danger" onClick={() => delData(data[0].row)}>
                                删除
                            </ElButton>
                        </>
                    )
                }
            }
        }
    }
])
const { tableRegister, tableState, tableMethods } = useTable({
    fetchDataApi: async () => {
        const { currentPage, pageSize } = tableState
        const res: any = await formTemplateList({
            pageNum: state.react ? 1 : unref(currentPage),
            pageSize: unref(pageSize),
            searchValue: state.keywords
        })
        console.log(res);
        state.react = false
        return {
            list: res.rows,
            total: res.total
        }
    },
    fetchDelApi: async () => {
        const res = await delTableListApi(unref(ids))
        return !!res
    }
})
// 表格头部信息
const headerCellStyle: any = {
    "background": "#F2F6FC",
    "height": "48px"
}
// 
const rowStyle: any = {
    "height": "48px"
}
const { loading, dataList, total, currentPage, pageSize } = tableState

const { getList, getElTableExpose, delList } = tableMethods

getList()
useEmitt({
    name: 'getList',
    callback: (type: string) => {
        if (type === 'add') {
            currentPage.value = 1
        }
        getList()
    }
})
const { allSchemas } = useCrudSchemas(crudSchemas)
const action = (row: any) => {
    console.log("编辑", row);
    dialogVisible1.value = true
    state.ruleForm = row
    state.ruleForm.templateValue=JSON.parse(state.ruleForm.templateValue)
    state.dialogVisible1Title = "编辑报名表模板"
}
const FromTableClick=(value:any)=>{
    console.log("回传信息：",value);
    state.ruleForm.templateValue=value
}
// 查询
const onSearch = () => {
    console.log("查询");
    state.react = true
    getList()
}
// 重置
const onReset = () => {
    console.log("重置");
    state.keywords = ""
    state.react = true
    getList()
}
const onAdd = () => {
    state.ruleForm = {}
    dialogVisible1.value = true
    state.dialogVisible1Title = t('新增报名表模板')
}

const ruleFormRef = ref<FormInstance>()
interface RuleForm {
    templateName: string
}
const rules = reactive<FormRules<RuleForm>>({
    templateName: [
        { required: true, message: '请输入报名表模板名称', trigger: 'blur' },
        { min: 1, max: 50, message: '请输入1~50字', trigger: 'blur' },
    ]
})
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loadings.value=true
            console.log('submit!')
            state.ruleForm.templateValue = JSON.stringify(state.ruleForm.templateValue)
            if (state.dialogVisible1Title == '新增报名表模板') {
                addformTemplateApi(state.ruleForm).then((res: any) => {
                    getList()
                    ElMessage({
                        type: 'success',
                        message: `${res.msg}`,
                    })
                    dialogVisible1.value = false
                    loadings.value=false
                }).catch((res: any) => {
                    ElMessage({
                        type: 'error',
                        message: `${res.msg}`,
                    })
                    loadings.value=false
                })
            } else {
                editformTemplateApi(state.ruleForm).then((res: any) => {
                    getList()
                    ElMessage({
                        type: 'success',
                        message: `${res.msg}`,
                    })
                    loadings.value=false
                    dialogVisible1.value = false
                }).catch((res: any) => {
                    ElMessage({
                        type: 'error',
                        message: `${res.msg}`,
                    })
                    loadings.value=false
                })

            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
const delData = (row: any) => {
    ElMessageBox.confirm('确认删除该报名表模板？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        // inputPattern:,
        draggable: true,
        type: 'warning',
    }).then(() => {
        deletepositionApi(row.id).then((res: any) => {
            getList()
            ElMessage({
                type: 'success',
                message: `${res.msg}`,
            })
        }).catch((res: any) => {
            ElMessage({
                type: 'error',
                message: `${res.msg}`,
            })
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消操作',
        })
    })
}
const resetForm = (formEl: FormInstance | undefined) => {
    dialogVisible1.value = false
    if (!formEl) return
    formEl.resetFields()
}

</script>
    
<style lang="less" scoped>
.message_p {
    padding-bottom: 16px;
}

h2 {
    position: relative;
    margin: 0;
    margin-bottom: 26px;

    &::after {
        position: absolute;
        left: -20px;
        top: 2px;
        content: "";
        width: 8px;
        height: 24px;
        background: #2F6CBC;
        border-radius: 0px 4px 4px 0px;
        opacity: 1;
    }
}
</style>